<?php declare(strict_types=1);


namespace PinWuYun\domain\service\starLinkServer;


use GuzzleHttp\Client;
use GuzzleHttp\Exception\GuzzleException;
use Psr\Http\Message\ResponseInterface;

class Common
{

    /**
     * 配置
    */
    public $config;

    public function __construct() {
        $this->config = new Config();
    }
    public $initZero = '0';


    /**
     * 正式环境
     * appKey：yd7fa6ccf40f5becef
     * appSecret：49c9498d47d24df3b3c4371c30940f18
     */

    /**
     *测试环境
     *  appkey：yd629822e4ac89f1a6
     *  secret：92f861c2ba794cda8825180cd4e19dbf
     *  @var string
     */

    protected $appKey = 'yd629822e4ac89f1a6';
    protected $appSecret = '92f861c2ba794cda8825180cd4e19dbf';

    /**
     * 生成请求参数
     * @param array $params 请求参数
     * @return array 处理后的参数
     */
    protected function genBaseParams(array $params): array
    {
        $params['appKey'] = $this->appKey;
        // 回调接口用的接收的
        $params['currentTime'] = $params['currentTime'] ?? $this->msTime();
        $sign = $this->geneSign($params);
        $params['sign'] = $sign;
        //  去除值为空的键
        $params = array_filter($params, function($row) {
            if($this->filterZero([$row])) {
                return true;
            }
            if(!$row) {
                return false;
            }
            return true;
        });
        return $params;
    }

    /**
     * 生成sign
     * @param array $params
     * @return string
     */
    protected function geneSign(array $params): string
    {
        // 参数筛选
        foreach($params as $key => $value) {
            if($this->filterZero([$value])) {
                continue;
            }
            if($key === 'sign' || empty($value)) {
                // 如果是键为sign或者值为空，则去除这个字段
                unset($params[$key]);
            }
        }
        // 参数排序
        ksort($params);

        // 参数拼接
        $tmp = [];
        foreach ($params as $key => $value) {
            $tmp[] = $key . '=' . $value;
        }
        $toBeSign = implode('&', $tmp);

        // 添加其他参数
        $toBeSign .= ('&'.$this->appSecret);
        return md5($toBeSign);
    }

    /**
     * 获取毫秒时间戳
     * @return float
     */
    protected function msTime(): float
    {
        list($msT, $sec) = explode(' ', microtime());
        return (float)sprintf('%.0f', (floatval($msT) + floatval($sec)) * 1000);
    }

    /**
     *  判断要用什么类型对 0 进行判断
     * @param array $border
     * @return bool
     */
    protected function filterZero(array $border): bool
    {
        $zero = $this->initZero;
        return $border[0] === $zero;
    }

    /**
     * 修改校验的0为什么类型
     * @param array $zero
     */
    public function initZero(array $zero)
    {
        $this->initZero = current($zero);
    }

    /**
     * 接口调用统一入口
     * @param string $method
     * @param string $uri
     * @param array $params
     * @return ResponseInterface
     * @throws GuzzleException
     */
    protected function start(string $method = 'get', string $uri = '', array $params = []): ResponseInterface
    {
        $uri = $this->config->url . $uri;
        // 参数处理
        $requestParam = $this->genBaseParams($params);
        // 发送请求
        $client = new Client();
        return $client->request($method, $uri, ['form_params' => $requestParam]);
    }

}